package com.imooc.rsa;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * Description: 加解密
 * Author: gzj
 * Date: 2024/4/24 13:37
 */
public class RsaUtil {
    private static final String PUBLICKEYSTR =  "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDUgoVw6BmuxG7zpXZKqEPHucv4XtOwLRxkTugvjZ6JQTx4tTP7oYJzdjbnP3F1aBcjEDrMrZqxzZOaxFnlStAe1Df2JlA3vHUeQ63rNGqMZwlxWkbLemaZsBDaX/6oe7Tqbvua5hEhlI65KJVJWX1Qr/qxVKm4T316DsM0fBm//wIDAQAB";
    private static final String PRIVATEKEYSTR =  "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBANSChXDoGa7EbvOldkqoQ8e5y/he07AtHGRO6C+NnolBPHi1M/uhgnN2Nuc/cXVoFyMQOsytmrHNk5rEWeVK0B7UN/YmUDe8dR5Dres0aoxnCXFaRst6ZpmwENpf/qh7tOpu+5rmESGUjrkolUlZfVCv+rFUqbhPfXoOwzR8Gb//AgMBAAECgYADj6JzVmMikmJIuD3WTllcbAKWF0rLhvEgB8wJ6VoDlJ+JtRISlXH5Q95uAmK4u651I6guJyza145/hgD6xWcreLapwS0qW2s0N2F92ehUwQz2UQIX6BeiZOXpXbievxjINMqpmUWBmYtu6CJ7bReRSsYWoz8WK5qE2cS8YXi1QQJBAP8ZeY6WTnY01dioOfeybxZjhNXqioB05vfV4qdJGAoXJ4qTUCDMFmpGgAqyagkZyNnhZ4dYy7UUjmytYjaaNOECQQDVQo886jtvN745Bf4zV6W5aKnYhGC8l61YeXrFiIGpepKoY1tGjsZ9GQAQ8cL8DpIQZdfoa6ohA6g4FAsZy7DfAkEA2c1TVuWIOi6kQqZnT8/ePvxbRxVIiaLGLOZF7VRgHQVl8lASgt4daUGmQvvPwtNsBHMKJKowyvuxGqLyjvIcoQJAYilO8UUHsnvADVNa3JsdZyhr9YOJW4Wdfrn77uyUckQ/OU7ydpnFd1OImkQN8iNqzC10okDcgHHEfnuLWIZPlQJBAOh0WWMgUeTUF1pAuRRXtm3zzXRrzVV429dkHooSs0RJP+nCsbr93Z5Q5iOcrTQ0QltF7tzbX+BYz6QJRrmevDg=";


    /**
     * 字节数组转Base64编码
     *
     * @param bytes 字节数组
     * @return Base64编码
     */
    private static String bytesToBase64(byte[] bytes) {
        byte[] encodedBytes = Base64.getEncoder().encode(bytes);
        return new String(encodedBytes, StandardCharsets.UTF_8);
    }

    /**
     * Base64编码转字节数组
     *
     * @param base64Str Base64编码
     * @return 字节数组
     */
    private static byte[] base64ToBytes(String base64Str) {
        byte[] bytes = base64Str.getBytes(StandardCharsets.UTF_8);
        return Base64.getMimeDecoder().decode(bytes);
    }

    /**
     * 公钥加密
     *
     * @param content:
     * @author: 405
     * @date: 2021/6/28
     * @return: java.lang.String
     */
    public static String encrypt(String content) {
        RSA rsa = new RSA(PRIVATEKEYSTR, PUBLICKEYSTR);
        byte[] encrypt = rsa.encrypt(StrUtil.bytes(content, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
//        System.out.println("公钥加密：" + bytesToBase64(encrypt));
        return bytesToBase64(encrypt);
    }

    /**
     * 私钥解密
     *
     * @param content:
     * @author: 405
     * @date: 2021/6/28
     * @return: java.lang.String
     */
    public static String decrypt(String content) {
        byte[] encrypt = base64ToBytes(content);
        RSA rsa = new RSA(PRIVATEKEYSTR, PUBLICKEYSTR);
        byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
//        System.out.println("私钥解密：" + new String(decrypt,StandardCharsets.UTF_8));
        return new String(decrypt,StandardCharsets.UTF_8);
    }

}
